package com.jnevision.notice.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnevision.dept.model.Dept;
import com.jnevision.dept.service.DeptServiceImpl;
import com.jnevision.notice.model.Notice;
import com.jnevision.notice.service.NoticeServiceImpl;
import com.jnevision.sysconfig.service.ConfigServiceImpl;

/**
 * <p>
 * 通知 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-14
 */
@RestController
@RequestMapping("/notices")
public class NoticeController extends ApiController {
    @Autowired
    private NoticeServiceImpl noticeServiceImpl;
    @Autowired
    private DeptServiceImpl deptServiceImpl;
    @Autowired
    private ConfigServiceImpl configServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody Notice notice) {
        notice.setValid(true);
        return this.success(this.noticeServiceImpl.save(notice));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody Notice notice) {
        return this.success(this.noticeServiceImpl.updateById(notice));
    }

    @DeleteMapping
    public R<Boolean> delete(Integer id) {
        return this.success(this.noticeServiceImpl.removeById(id));
    }

    @GetMapping
    public R<Notice> get(Integer id) {
        this.browsing(id);
        return this.success(this.noticeServiceImpl.getById(id));
    }

    @GetMapping("/page")
    public R<IPage<Notice>> page(Page<Notice> page, Notice notice) {
        String outUrl = this.configServiceImpl.getValue("notice_url");
        IPage<Notice> pageNotice = this.noticeServiceImpl.page(page,
            new QueryWrapper<Notice>().gt(StringUtils.isNoneBlank(notice.getStart()), "create_time", notice.getStart())
                .lt(StringUtils.isNoneBlank(notice.getEnd()), "create_time", notice.getEnd())
                .like(StringUtils.isNotBlank(notice.getTheme()), "theme", notice.getTheme())
                .eq(null!=notice.getDeptId()&&notice.getDeptId() > 0, "dept_id", notice.getDeptId()).orderByDesc("weight"));
        pageNotice.getRecords().forEach(nt -> {
            nt.setOutUrl(outUrl + nt.getId());
        });

        return this.success(pageNotice);
    }

    /**
     * 点击量
     * 
     * @param id
     * @return
     */
    @GetMapping("/browsing")
    public R<Integer> browsing(Integer id) {
        Notice notice = this.noticeServiceImpl.getById(id);
        notice.setBrowsing(notice.getBrowsing() + 1);
        this.noticeServiceImpl.updateById(notice);
        return this.success(this.noticeServiceImpl.getById(id).getBrowsing());
    }

    /**
     * 根据当前用户获取所需通知
     *
     * @param token
     * @return
     */
    @GetMapping("/user")
    public R<List<Notice>> getByDept(@RequestHeader("token") String token) {
        Dept dept = this.deptServiceImpl.getProject(token);

        List<Notice> noticeList = this.noticeServiceImpl.list(new QueryWrapper<Notice>().eq("dept_id", dept.getId()));

        String outUrl = this.configServiceImpl.getValue("notice_url");
        noticeList.forEach(nt -> {
            nt.setOutUrl(outUrl + nt.getId());
        });

        return this.success(noticeList);
    }
}
